<template>
  <div class='container'>
    <el-dialog
      :title="bigTitle"
      :visible="showDialog"
      width="30%"
      @close="close"
      >
      <el-form ref="deptForm" :model="deptForm" :rules="deptRules">
        <el-form-item v-if="!this.addlist" label="所属学科" prop="subjectID">
          <el-select v-model="deptForm.subjectID" size="mini" placeholder="请选择" style="width:80%">
            <el-option
            v-for="item in lableList"
            :key="item.id"
            :label="item.label"
            :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input v-model="deptForm.tagName" size="mini" placeholder="请输入目录名称" style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirmClick">确 定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '../../api/hmmm/subjects'
import { add, detail, update } from '../../api/hmmm/tags'
export default {
  props: {
    addlist: {
      type: String,
      default: null
    },
    showDialog: {
      type: Boolean, // 类型
      default: false // 设置默认值
    },
    addId: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      deptForm: {
        // subjectName: '',
        subjectID: '',
        tagName: '' // 目录名称
      },
      lableList: [],
      // 检验规则
      deptRules: {
        subjectID: [
          { required: true, trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    bigTitle () {
      return this.addId ? '修改目录' : '新增目录'
    }
  },
  created () {
    this.getLableList()
  },
  methods: {
    // 回显数据
    async getVoreData () {
      const { data } = await detail({ id: this.addId })
      this.deptForm.tagName = data.tagName
      this.deptForm.subjectID = data.subjectID
    },
    // 点击确认按钮
    confirmClick () {
      // 表单校验
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          if (this.addId) {
            await update({ ...this.deptForm, id: this.addId })
            this.$message.success('修改完成')
          } else {
            if (this.$route.query.id) {
              await add({ ...this.deptForm, subjectID: +this.$route.query.id })
            } else {
              await add(this.deptForm)
            }
            this.$message.success('添加成功')
          }
          this.$emit('addDepatList')
          this.close()
        }
      })
    },
    // 获取学科列表数据
    async getLableList () {
      const { data } = await simple()
      this.lableList = data
    },

    // 关闭弹层
    close () {
      this.deptForm = {
        subjectID: '',
        tagName: ''
      }
      // 清空表单
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style scoped lang='less'></style>
